home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3933 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: urals.jpl.nasa.gov!user
  2. From: mlj@tazboy.jpl.nasa.gov (Mose L. Johnson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Unresolved Templates during link time in Borderland 4.5
  5. Date: Fri, 26 Jan 1996 12:51:04 -0800
  6. Organization: Jet Propulsion Laboratory
  7. Message-ID: <mlj-2601961251040001@urals.jpl.nasa.gov>
  8. References: <mlj-2201960838180001@urals.jpl.nasa.gov>
  9. NNTP-Posting-Host: urals.jpl.nasa.gov
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. This reply should have been posted to this Newsgroup.
  15.  
  16. Organization: Infosoft Inc., Cupertino, CA, USA
  17.  
  18. The problem is that, when the functions in your template class are compiled,
  19. you have to tell the compiler which versions are needed.  I have found two
  20. ways that work with Borland C++.
  21.  
  22. 1.  The "lazy person's way" -- Just put both the header and the function
  23. bodies together in one file, and include that file wherever you use the
  24. template class.  Of course, this will cause the template class's functions to
  25. be compiled multiple times, but the linker will throw out all but one copy.
  26.  
  27. 2.  To put the class in a .LIB file:  Say its name is TClass.
  28.  
  29. TClass.hpp (the header to be included where it is used) contains:
  30.  
  31.     #ifndef TCLASS_HPP
  32.     #define TCLASS_HPP
  33.  
  34.     #ifndef TCLASS_CPP
  35.     #pragma option -Jgx     // Compile all function refs as external, except
  36.                             // when compiling TClass.cpp itself.
  37.     #endif
  38.  
  39.     template <class T> class TClass {
  40.         /* body */
  41.     };
  42.  
  43.     /* "inline" functions, if any */
  44.  
  45.     #endif  // TCLASS_HPP
  46.  
  47. TClass.cpp (which compiles to become the library module) contains:
  48.  
  49.     #pragma option -Jgd     // Compile function bodies here and export them!
  50.  
  51.     #define TCLASS_CPP
  52.  
  53.     #include "TClass.hpp"
  54.  
  55.     /* function bodies */
  56. --
  57. John David Galt                                I do not speak for my employer.
  58. jgalt@infosoft.com      Send personal mail to:  John_David_Galt@cup.portal.com
  59.